﻿# Create your views here.
from login import Login
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
from django.views.decorators.csrf import ensure_csrf_cookie
from forms import *
from bazy.forms import CreateOffenseTypeForm
from bazy.forms import  CreateEventForm
from models import  *
from PIL import Image


def login(request,from_url):
	log = Login(request);
	if log.isLogged() is not True:
		if request.method=='POST':
			form = LoginForm(request.POST)
			if form.is_valid():
				username = request.POST['username']
				password = request.POST['password']
				if log.login(username, password):
					return redirect('/'+from_url)
				else:
					return render(request, 'template.html',{'main_body':'login_form.html','from_url':from_url,'form':form,'invalid_data':True,'is_not_logged':False})
			else:
				return render(request, 'template.html',{'main_body':'login_form.html','from_url':from_url,'form':form,'invalid_data':True,'is_not_logged':False}) 
		else:
			form =LoginForm()
			return render(request, 'template.html',{'main_body':'login_form.html','from_url':from_url,'form':form,'is_not_logged':True})
	else:
		return redirect('/'+from_url)
	
def index(request):
	log = Login(request);
	if log.isLogged():
		return render(request, 'template.html',{'main_body':'index.html','from_url':'index','menu':'index'})
	return redirect('/login/index')
		
def logout(request,from_url):
	log = Login(request);
	log.logout()
	return redirect('/'+from_url)

def showAllUsers(request):
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'user/show/all')
	form = AddUserForm()
	return render(request, 'template.html',{'main_body':'show_all_users.html','from_url':'user/show/all','form':form,'menu':'users'})


def showAllHools(request):
	
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'user/show/all')
	form = CreateHooliganForm()
	return render(request, 'template.html',{'main_body':'show_all_hools.html','from_url':'user/show/all','form':form,'menu':'hools'})


def showHooligan(request,hool_id):
	log = Login(request)
	hooligan = Hooligan.objects.get(pk=hool_id)
	form = AddEventToHooligan()
	if log.isLogged() is not True:
		return redirect('/login/'+'user/show/all')
	return render(request, 'template.html',{'hooligan':hooligan,'main_body':'hools_profile.html','form':form,'from_url':'hools/show/','menu':'none'})

def cropHooliganImage(request,hool_id):
	log = Login(request)
	hooligan = Hooligan.objects.get(pk=hool_id)
	
	if log.isLogged() is not True:
		return redirect('/login/'+'hools/edit/'+hool_id)
	
	if request.method=='POST':
		if request.POST.get('width',0)=='' or request.POST.get('height',0)=='':
			return redirect('/hools/edit/'+hool_id)
		im = Image.open('imgs/'+hooligan.photoUrl)
		im=im.crop([int(request.POST.get('x',0)),int(request.POST.get('y',0)),int(request.POST.get('x',0))+int(request.POST.get('width',0)),int(request.POST.get('y',0))+int(request.POST.get('height',0))])
		im.save('imgs/'+hooligan.photoUrl)
	return redirect('/hools/edit/'+hool_id)

def editHooligan(request,hool_id):
	log = Login(request)
	hooligan = Hooligan.objects.get(pk=hool_id)
	
	
	
	if log.isLogged() is not True:
		return redirect('/login/'+'hools/edit/'+hool_id)
	
	if request.method=='POST':
		im = Image.open('imgs/'+hooligan.photoUrl)
		im=im.crop([int(request.POST.get('x',0)),int(request.POST.get('y',0)),int(request.POST.get('x',0))+int(request.POST.get('width',0)),int(request.POST.get('y',0))+int(request.POST.get('height',0))])
		im.save('imgs/'+hooligan.photoUrl)
	return render(request, 'template.html',{'hooligan':hooligan,'main_body':'edit_hooligan.html','from_url':'hools/show/','menu':'none'})

def showAllEvents(request):
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'events/show/all')
	form= CreateEventForm()
	addHooliganForm=AddHooliganToEvent()
	return render(request, 'template.html',{'main_body':'show_all_events.html',
										 'form':form,
										 'addHooliganToEvent':addHooliganForm,
										 'from_url':'hools/show/',
										 'menu':'events'})

def showEvent(request,event_id):
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'events/show/all'+eventId)
	event= Event.objects.get(pk=event_id)
	
	return render(request, 'template.html',{'main_body':'show_event.html','event':event,'from_url':'hools/show/','menu':'events'})

def showAllOffense(request):
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'events/show/all'+eventId)
	offenseForm= CreateOffenseTypeForm()
	return render(request, 'template.html',{'main_body':'show_all_offense_type.html','offenseForm':offenseForm,'from_url':'hools/show/','menu':'offense'})

def mobile(request):
	log = Login(request)
	if log.isLogged() is not True:
		return redirect('/login/'+'events/show/all'+eventId)
	offenseForm= CreateOffenseTypeForm()
	return render(request, 'mobile.html')

def mobileLogin(request):
	log = Login(request);
	if log.isLogged() is not True:
		if request.method=='POST':
			form = LoginForm(request.POST)
			if form.is_valid():
				username = request.POST['username']
				password = request.POST['password']
				if log.login(username, password):
					return redirect('/')
				else:
					return render(request, 'template.html')
			else:
				return render(request, 'template.html') 
		else:
			form =LoginForm()
			return render(request, 'template.html')
	else:
		return redirect('/'+from_url)